$(function(){
	//绑定所有的关闭图片点击关闭当前的层
	$(".tdtitle span").each(function(){
		$(this).on("click",function(){
			$(this).parents(".tdpopup").addClass("hide");
		});
	});
	var showCount = 7;
	
	$("#allowance-search").on("click",function(){
		$("#pagenumber").val(1);
		if("" == $("#sdate").val() || "" == $("#edate").val()){
			layer.msg("请输入补贴查询时间！（查询时间从2015-01-01起）",2,8);
			return false;
		}
		
		$.ajax({
			url:"allowanceList",
			type:"POST",
			dataType:"json",
			data:{
				"startdate":$("#sdate").val(),
				"enddate":$("#edate").val(),
				"aid":$("#aid").val(),
				"page":$("#pagenumber").val()
			},
			success:function(data){
				var html = [];
				if(data.rebateMoney<=0){
					layer.msg("暂无数据！（查询时间从2015-01-01起）",2,8);
					return false;
				}else{
					$("#allowanceDiv").removeClass('hide');
					$("#allowanceListCount").val(data.count);
					$("#backBillCount").text(data.count);
					$("#backMoney").text(data.rebateMoney);
					$("#rebate_stardate").text($("#sdate").val());
					$("#rebate_enddate").text($("#edate").val())
					$("#Pagination").myPagination({
						ajax:{
							on : true,
							url : "allowanceList",
							type : "POST",
							dataType : "json",
							param:{
								on : true,
								startdate:$("#sdate").val(),
								enddate:$("#edate").val(),
								aid:$("#aid").val(),
								page:$("#pagenumber").val(),
								pageSize:10
							},
							callback:function(data){
								fillData(data);
							}
						}
					});
				}
			}
		});
	});
});


///true 即第一个时间大于第二时间（即之后）
function  compareDate(dateA,dateB){
	var sdateArr = dateA.split("-")
	var startdate = new Date(sdateArr[0],sdateArr[1],sdateArr[2]);
	var edateArr = dateB.split("-")
	var enddate = new Date(edateArr[0],edateArr[1],edateArr[2]);
	if(startdate.getTime() > enddate.getTime()){
		return true;
	}else{
		return false;
	}	
}



function fillData(data){
	$("#backBillCount").text(data.count);
	var html = [];
	if(null != data.allowanceList && data.allowanceList.length > 0){
		for (var i = 0; i < data.allowanceList.length; i++) {
			var obj = data.allowanceList[i];
			html.push('<tr>');
			html.push('<td style="text-align:center">' + (obj.orderNumber == undefined ? '' : obj.orderNumber) + '</td>');
			html.push('<td style="text-align:center">' + (obj.sdate == undefined ? '' : obj.sdate) + '</td>');
			html.push('<td style="text-align:center">' + (obj.isaccount == 0 ? '' : "已发放") + '</td>');
			html.push('<td style="text-align:center">' + (obj.earningtype == 4 ?"营销平台赠送":'') + '</td>');
			html.push('<td style="text-align:center">' + (obj.cmamount == undefined ? '':obj.cmamount)+ '</td>');  //金额
			html.push('<td style="text-align:center">' + (obj.remark == undefined ? '':obj.remark)+ '</td>');  //备注
			html.push('</tr>');
		}
		$("#allowanceTable > tbody").html(html.join(''));
		$('#allowanceTable > tbody > tr:even td').css("background", "#FFF");
	}else{
		html.push('<tr>');
		html.push('<td colspan="6" id="none"><span style="margin-left:45%">(此时间段)暂无数据</span>');
		html.push('</td>');
		html.push('</tr>');	
		$("#allowanceTable > tbody").html(html.join(''));
		$('#allowanceTable > tbody > tr:even td').css("background", "#FFF");
	}
}

